package org.lds.message.center.admin.business.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.lds.message.center.admin.business.app.entity.App;
import org.lds.message.center.admin.business.app.mapper.AppMapper;
import org.lds.message.center.admin.business.template.entity.Template;
import org.lds.message.center.admin.business.template.mapper.TemplateMapper;
import org.lds.message.center.admin.business.template.service.TemplateService;
import org.lds.message.center.admin.common.enums.AdminErrorEnum;
import org.lds.message.center.common.model.ResultDTO;
import org.lds.message.center.common.model.request.GetTemplateAdminRequest;
import org.lds.message.center.common.model.response.GetTemplateAdminResponse;
import org.lds.message.center.common.util.AssertUtil;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 服务实现类
 *
 * @author lidongsheng
 * @since 2024-12-22
 */
@Service
@RequiredArgsConstructor
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements TemplateService {
    private final AppMapper appMapper;
    private final TemplateMapper templateMapper;

    @Override
    public ResultDTO<GetTemplateAdminResponse> getTemplate(GetTemplateAdminRequest request) {
        App app = appMapper.selectOne(new QueryWrapper<>(App.builder().name(request.getAppName()).enable(true).build()));
        AssertUtil.assertTrue(Objects.nonNull(app), AdminErrorEnum.APP_NOT_EXIST);

        Template template = templateMapper.selectOne(new QueryWrapper<>(Template.builder()
                .appId(app.getId())
                .name(request.getTemplateName())
                .enable(true)
                .build()));
        AssertUtil.assertTrue(Objects.nonNull(template), AdminErrorEnum.TEMPLATE_NOT_EXIST);
        return ResultDTO.success(GetTemplateAdminResponse.builder()
                        .subject(template.getSubject())
                        .text(template.getText())
                        .build());
    }
}
